/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.clebersantos.dao;

import com.clebersantos.pojos.Usuario;
import com.clebersantos.utils.HibernateUtil;
import java.io.Serializable;
import org.apache.derby.vti.Restriction;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Jurema
 */
public class UsuarioDao implements Serializable {

    private Session sessao;
    private Transaction tx;
    private Usuario user;

    public String cadastrarUsuario(Usuario usuario) {

        try {
            sessao = HibernateUtil.getSessionFactory().openSession();
            tx = sessao.beginTransaction();

            user = usuario;
            sessao.save(user);
            tx.commit();
            
            user = new Usuario();
            
            return "Usuário cadastrado com sucesso!";
        } catch (Exception e) {
            return "Erro ao cadastrar usuário! " + e;
        } finally {
            sessao.close();
        }
    }

    public boolean logarSistema(Usuario usuario) {
        boolean retorno = false;
        
        try {
            sessao = HibernateUtil.getSessionFactory().openSession();
            
            Criteria cri = sessao.createCriteria(Usuario.class)
                    .add(Restrictions.eq("login", usuario.getLogin()))
                    .add(Restrictions.eq("senha", usuario.getSenha()));
            
            if(!cri.uniqueResult().equals(null) || !cri.uniqueResult().equals("")){
                retorno = true;
            }
            
            return retorno;
            
        } catch (Exception e) {
            e.printStackTrace();
            return retorno;
        } finally {
            sessao.close();
        }

    }

}
